<?php
defined('JOC') or die;

class Template
{
  var $file       = ""        ;
  var $path       = ""        ;
  var $trans      = array()   ;
  var $script     = array()   ;
  var $got_script	= false     ;
  var $css        = array()   ;
  var $got_css    = false     ;

  function __construct($template_file='',$theme='default',$module='')
  {
    $this->add_file($template_file,$theme,$module);
    return $this ;
  }
  function add_key($key,$text,$html=false)
  {
    if($html){
      $this->extract_js_css($text) ;
    }
    $this->file = str_replace($key,$text,$this->file) ;
  }
  function add_array($array)
  {
    foreach($array as $key => $value)
    {
      $this->file = str_replace($key,$value,$this->file) ;
    }
  }
  function imp()
  {
    $this->add_js_css();
    echo utf8_encode(strtr($this->file, $this->trans)) ;
  }
  function get_string()
  {
    $this->add_js_css() ;
    return utf8_encode(strtr($this->file, $this->trans)) ;
  }
  function set_string($string='')
  {
  	$this->file = $string ;
  	return $this ;
  }
  function convert()
  {
    return strtr($this->file, $this->trans);
  }
  function add_file($template_file = '' , $theme = 'default' , $module = '' )
  {
    if($template_file)
    {
      $this->path = ROOT . "/themes/$theme/" . ($module ? $module.'/' : '' ) . $template_file ;
      if (file_exists($this->path))
      {
        $this->file .= file_get_contents( $this->path , true );
      }
      else
      {
        $this->path = ROOT."/themes/$theme/$template_file" ;
        if (file_exists($this->path))
        {
          $this->file .= file_get_contents($this->path, true);
        }
        else
        {
//        debug::pr("not found file : ".ROOT."themes/default/$template_file",'template',false);
        }
      }
    }

    return $this ;
  }
  function extract_js_css($text)
  {
    $result = $this->get_config_json($text);
    $script = explode(',',$result['js']);
    foreach($script as $aux)
    {
      if(!(in_array($aux,$this->script)))
      {
        array_push($this->script,$aux) ;
        $this->got_script = true ;
      }
    }
    $css = explode(',',$result['css']);
    foreach($css as $aux)
    {
      if(!(in_array($aux,$this->css)))
      { 
        array_push($this->css,$aux) ;
        $this->got_css = true ;
      }
    }
//		debug::pr($this->script,'js',false);
//		debug::pr($this->css,'css',false);
  }
  function add_js_css()
  {
    if($this->got_script) $this->add_key('<!--#SCRIPT#-->','<script type="text/javascript" src="'.JS_URL.'?tag='.implode(',',$this->script).'" ></script> ');
    if($this->got_css) $this->add_key('<!--#STYLE#-->','<link rel="stylesheet" type="text/css" href="'.CSS_URL.'?'.( BACKEND ? 'backend=1&' : '' ).'tag='.implode(',',$this->css).'" /> ');
  }
  function addModuleToRegion( $string = '', $section = 0 )
  {
    if($section)
    {
      $section = "{SECTION_" . $section . "}";
      $string .= $section ;
      $this->add_key( $section , $string ) ;
      
    }
    return $this ;
  }
  function get_content_string()
  {
    $start = strpos($text,"config-->");
    return substr($text,$start);
  }
  function get_config_string($text)
  {
    $stop = strpos($text,"config-->");
    return substr($text,0,$stop);
  }
  function get_config_json($text)
  {
    $start = strpos($text,"<!--config");
    $stop = strpos($text,"config-->");
    $result = substr($text,($start+10),($stop - $start)-11);
    return json_decode($result,true);
  }
}
